package com.rightsidetech.http.controller.agentApp.home;

import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.system.dtos.response.store.HomeStoreStorkResp;
import com.rightsidetech.system.service.SysAgentInfoService;
import com.rightsidetech.system.service.SysStoreStorkService;
import com.rightsidetech.system.service.SystemParamService;
import com.rightsidetech.trade.dtos.response.HomeAgentSpscSaleResp;
import com.rightsidetech.trade.dtos.response.HomeMonthOrderResp;
import com.rightsidetech.trade.dtos.response.HomeSpscSaleResp;
import com.rightsidetech.trade.service.TdCommodityUserOrderService;
import com.sun.javafx.collections.MappingChange;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @Package: com.rightsidetech.http.controller.agentApp.home
 * @ClassName: AgentAppHomeController
 * @Author: yinj
 * @CreateTime: 2021/12/24 10:00
 * @Description: 代理商app首页数据统计
 */
@RestController
@RequestMapping("/agentApp/home")
@ShenyuSpringMvcClient(path = "/agentApp/home/**")
@Api(tags = "首页数据统计")
public class AgentAppHomeController extends BaseController {

    @Resource
    private TdCommodityUserOrderService tdCommodityUserOrderService;
    @Resource
    private SysAgentInfoService sysAgentInfoService;
    @Resource
    private SysStoreStorkService sysStoreStorkService;

    @Log
    @Token
    @ApiOperation("本月订单统计")
    @GetMapping("/this_month_order")
    public ResponseData<List<HomeMonthOrderResp>> thisMonthOrder(){
        Long agentLoginId = Long.parseLong(getAttribute());
        //获取登录账号代理商id
        Long agentInfoId = sysAgentInfoService.findAgentInfoId(agentLoginId);
        if (StringUtils.isEmpty(agentInfoId)){
            return ResponseData.fail(CodeIdEnum.Expired);
        }
        return tdCommodityUserOrderService.thisMonthOrder(agentInfoId);
    }


    @Log
    @Token
    @ApiOperation("缺货门店统计")
    @GetMapping("/outof_stock_store")
    public ResponseData<List<HomeStoreStorkResp>> outofStockStore(){
        Long agentLoginId = Long.parseLong(getAttribute());
        //获取登录账号代理商id
        Long agentInfoId = sysAgentInfoService.findAgentInfoId(agentLoginId);
        if (StringUtils.isEmpty(agentInfoId)){
            return ResponseData.fail(CodeIdEnum.Expired);
        }
        return sysStoreStorkService.outofStockStore(agentInfoId);
    }

    @Log
    @Token
    @ApiOperation("当日电池销售按型号统计")
    @GetMapping("/today_specSale")
    public ResponseData<HomeAgentSpscSaleResp> todaySpecSale(){

        Long agentLoginId = Long.parseLong(getAttribute());
        //获取登录账号代理商id
        Long agentInfoId = sysAgentInfoService.findAgentInfoId(agentLoginId);
        if (StringUtils.isEmpty(agentInfoId)){
            return ResponseData.fail(CodeIdEnum.Expired);
        }

        List<HomeSpscSaleResp> list = tdCommodityUserOrderService.todaySpecSale(null, agentInfoId);
        if (CollectionUtils.isEmpty(list)){
            return ResponseData.success();
        }
        Long totalNum = list.stream().map(HomeSpscSaleResp::getNum).reduce(0L, Long::sum);
        HomeAgentSpscSaleResp saleResp = new HomeAgentSpscSaleResp();
        saleResp.setHomeSpscSales(list);
        saleResp.setTotalNum(totalNum);
        return ResponseData.success(saleResp);
    }

}
